package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.constant.RedisMessageConstant;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.Order;
import com.itxiaowu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 *  预约类
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;
    /**
     *  提交预约信息
     */
    @RequestMapping("/submitorder.do")
    public Result submitOrder(@RequestBody Map map){
        // 1: 先比较验证码
        // 1.1: 获得验证码
        String validateCode = String.valueOf(map.get("validateCode"));
        // 1.2: 从内存中获取验证码
        String telephone = String.valueOf(map.get("telephone"));
        //    内存中的数据是电话号码+信息
        Jedis jedis = jedisPool.getResource();
        String code = jedis.get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        jedis.close();

        //设置预约状态
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        map.put("orderStatus",Order.ORDERSTATUS_NO);

        //    -- > 判断是否相等
        if(!validateCode.equals(code) || code == null){
            // 不想等
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);

        }

        Result result = null;
        try {// 2: 调用服务层进行处理

            result = orderService.submitOrder(map);
            // 3: 判定是否返回的数据是否为false
            if(result.isFlag()){
                //为真则发送短信
                //    获得日期数据
                String orderDate = String.valueOf(map.get("orderDate"));
                System.out.println("日期为:" + orderDate);
//            try {
////                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,orderDate);
//            } catch (ClientException e) {
//                e.printStackTrace();
//            }
            }

            return result;

        }catch (Exception e) {
            e.printStackTrace();
            return result = new Result(false,MessageConstant.SEND_VALIDATECODE_SUCCESS);   // -- > 返回的空数据, 只是一个占位数
        }

    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id){

        try{
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,map);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }
}
